﻿using System;
using System.Globalization;
using ContractManagementSystem.BusinessLogicLayer;
using ContractManagementSystem.EntityObjects;

namespace ContractManagementSystem.Presentation.ProfileManagement
{
    public partial class UpdateContractor : System.Web.UI.Page
    {
        private ContractorEntity _contractor;

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckUserPermission();
            if (!IsPostBack)
                LoadUpdateContractor();
        }

        private void LoadUpdateContractor()
        {
            var id = Request.QueryString["id"];
            if (!string.IsNullOrEmpty(id))
            {
                var dataTable = ContractorProfileLogic.GetContractorByContractorId(id);
                textContractorId.Text = id;
                textUserName.Text = dataTable.Rows[0]["UserName"].ToString();
                textFullName.Text = dataTable.Rows[0]["FullName"].ToString();
                textPhoneNumber1.Text = dataTable.Rows[0]["PhoneNumber1"].ToString();
                textPhoneNumber2.Text = dataTable.Rows[0]["PhoneNumber2"].ToString();
                textAddress.Text = dataTable.Rows[0]["Address"].ToString();
                textEmail.Text = dataTable.Rows[0]["Email"].ToString();
                textBranch.Text = dataTable.Rows[0]["Branch"].ToString();
                textSortCode.Text = dataTable.Rows[0]["SortCode"].ToString();
            }
        }

        protected void ButtonSaveClick(object sender, EventArgs e)
        {
            _contractor = new ContractorEntity
            {
                ContractorId = int.Parse((textContractorId.Text),CultureInfo.InvariantCulture),
                UserName = textUserName.Text,
                FullName = textFullName.Text,
                Address = textAddress.Text,
                PhoneNumber1 = textPhoneNumber1.Text,
                PhoneNumber2 = textPhoneNumber2.Text,
                Email = textEmail.Text,
                Branch = textBranch.Text,
                SortCode = textSortCode.Text
            };
            var result = ContractorProfileLogic.UpdateContractor(_contractor);
            if (result != null)
            {
                Response.Redirect("ListContractor.aspx");
            }
            else
            {
                var userEventEntity = new EventEntity
                {
                    UserName = CommonLogic.ConvertToString(Session["UserName"]),
                    EventType = (int)EventTypeList.UserEvent,
                    OnDate = DateTime.Now,
                    Description = "User update failed"
                };
                EventTracer.SaveEvents(userEventEntity);
            }
        }

        // Check user's permissions
        private void CheckUserPermission()
        {
            var userName = CommonLogic.ConvertToString(Session["UserName"]);
            const int functionId = (int)FunctionList.UpdateContractor;

            var urlToCome = Page.Master.ResolveUrl("Presentation/AuthorizationError.aspx");

            AuthorizationChecker.IsValidAuthorization(userName, functionId, Response, urlToCome);
        }

    }
}
